package com.galo.service.controller;

import com.alibaba.fastjson.JSONObject;
import com.galo.service.common.Result;
import com.galo.service.common.ResultGenerator;
import com.galo.service.service.AirConditionerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 智能空调
 * @Auther: lxlin
 * @Date: 2018/11/15 16:53
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/air")
public class AirConditionerController {

    @Autowired
    private AirConditionerService airConditionerService;

    /**
     * 智能空调开关控制
     * @param devsn 设备标识
     * @param sta 状态
     */
    @PostMapping("/control")
    public Result control(@RequestBody String body) {
        JSONObject object = JSONObject.parseObject(body);
        String devsn = object.getString("devsn");
        String sta = object.getString("sta");
        airConditionerService.control(devsn, sta);
        return ResultGenerator.genSuccessResult("指令发送成功！");
    }

    /**
     * 切换空调品牌接口
     * @param devsn 空调设备编号
     * @param brand 品牌编号
     */
    @PostMapping("/brand")
    public Result brand(@RequestBody String body){
        JSONObject object = JSONObject.parseObject(body);
        String devsn = object.getString("devsn");
        int brand = object.getIntValue("brand");
        airConditionerService.brand(devsn,brand);
        return ResultGenerator.genSuccessResult("指令发送成功！");
    }

    /**
     * 空调温度调节接口
     * @param devsn 空调设备编号
     * @param tmp 目标温度
     */
    @PostMapping("/tempering")
    public Result tempering(@RequestBody String body){
        JSONObject object = JSONObject.parseObject(body);
        String devsn = object.getString("devsn");
        int tmp = object.getIntValue("tmp");
        airConditionerService.tempering(devsn,tmp);
        return ResultGenerator.genSuccessResult("指令发送成功！");
    }

    /**
     * 空调模式切换接口
     * @param devsn 空调设备编号
     * @param mode 空调模式
     */
    @PostMapping("/mode")
    public Result mode(@RequestBody String body){
        JSONObject object = JSONObject.parseObject(body);
        String devsn = object.getString("devsn");
        String mode = object.getString("mode");
        airConditionerService.mode(devsn,mode);
        return ResultGenerator.genSuccessResult("指令发送成功！");
    }
}
